package com.etc.controller;

import com.etc.entity.User;
import com.etc.service.UserInfoService;
import com.etc.util.Layui;
import com.github.pagehelper.PageInfo;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("/userInfoController")
public class userInfoController {

    Logger logger=Logger.getLogger(userInfoController.class);
    @Autowired
    UserInfoService service;

    /**
     * 根据用户编号修改状态
     * 邓玲琪
     * @return
     */
    @RequestMapping("/modifyStatus")
    @ResponseBody
    public int modifyStatus(@RequestParam(value = "userId")String userId,
                            @RequestParam(value = "status",defaultValue = "0")int status){
        //调用业务层
        int fian = service.modifyStatus(userId,status);
        return fian;
    }
    /**
     * 分页 按条件模糊查找所有用户信息：用户名，姓名，电话
     * 邓玲琪
     * @param userName userRealName userTel
     *@param pageNum
     *@param pageSize
     *@return PageInfo<UserInfo>
     */
    @RequestMapping("/getqueryAllUserInfo")
    @ResponseBody
    public Layui getqueryAllUserInfo(@RequestParam(value = "pageNum",defaultValue = "1")int pageNum,
                                     @RequestParam(value = "pageSize",defaultValue = "3")int pageSize,
                                     @RequestParam(value = "userName",defaultValue = "")String userName,
                                     @RequestParam(value = "userRealName",defaultValue = "")String userRealName,
                                     @RequestParam(value = "userTel",defaultValue = "")String userTel){
        PageInfo<User>  pageInfo=service.getqueryAllUserInfo(pageNum,pageSize,userName,userRealName,userTel);
        return Layui.data(pageInfo.getTotal(),pageInfo.getList());
    }
    /**
     * 用户登录
     * 陆海
     */
    @RequestMapping("/login")
    public ModelAndView login(@RequestParam(value = "LuserName",defaultValue = "")String LuserName,
                              @RequestParam(value = "LuserPwd",defaultValue = "")String LuserPwd,
                              HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ModelAndView mv=new ModelAndView();
        User user=service.getLogin(LuserName,LuserPwd);
        if (user!=null){
            logger.debug("姓名:"+LuserName+",密码:"+LuserPwd);
            request.getSession().setAttribute("user",user);
            request.getRequestDispatcher("../index.jsp").forward(request,response);
            return null;
        }else{
            logger.debug("登录失败");
            mv.setViewName("login");
            return mv;
        }
    }
    /**
     * 用户注册
     * 陆海
     */
    @RequestMapping("/register")
    public ModelAndView register(User user,MultipartFile userfile,
                           HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.debug(user);
        //2.获取原始的文件名
        String filename = userfile.getOriginalFilename();
        //3.定义文件存储位置
        File file = new File("D:\\uploadFile\\" + filename);
        //4.写入
        try {
            userfile.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        user.setUserImg(filename);
        ModelAndView mv=new ModelAndView();
        boolean flag = service.addUser(user);
        if (flag) {
            logger.debug("注册成功");
            request.getRequestDispatcher("../index.jsp").forward(request,response);
            return null;
        } else {
            logger.debug("注册失败");
            mv.setViewName("login");
            return mv;
        }
    }

    /**
     * 初始化密码
     * 邓玲琪
     * @return
     */
    @RequestMapping("/InitializeTheUserPwd")
    @ResponseBody
    public int InitializeTheUserPwd(@RequestParam(value = "userId")String userId){
        //调用业务层
        int fian = service.InitializeTheUserPwd(userId);
        return fian;
    }

    /**
     * 用户修改个人信息
     * 陆海
     */
    @RequestMapping("/changeUserInfo")
    public ModelAndView changeUserInfo(User user,MultipartFile userfile,
                                 HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.debug(user);
        //2.获取原始的文件名
        String filename = userfile.getOriginalFilename();
        //3.定义文件存储位置
        File file = new File("D:\\uploadFile\\" + filename);
        //4.写入
        try {
            userfile.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        user.setUserImg(filename);
        ModelAndView mv=new ModelAndView();
        boolean flag = service.updateUserInfo(user);
        mv.setViewName("userCenter");
        return mv;


    }

    /**
     * 用户修改密码
     * 陆海
     */
    @RequestMapping("/changeUserPassword")
    public ModelAndView changeUserPassword(String userId,@RequestParam("password") String password,@RequestParam("newPassword") String newPassword) {
        ModelAndView mv=new ModelAndView();
        boolean flag = service.updateUserPassword(userId, password, newPassword);
        mv.setViewName("userCenter");
        return mv;
    }
    /**
     * 用户修改退出
     * 邓玲琪
     */
    @RequestMapping(value = "/exit")
    public ModelAndView exit(HttpServletRequest request) {
        /*request.getSession().removeAttribute("user");
        request.removeAttribute("user");
        ModelAndView mvw = new ModelAndView();
        return new ModelAndView("redirect:/index.jsp");*/
        request.getSession().removeAttribute("user");
        ModelAndView mv = new ModelAndView();
        mv.setViewName("redirect:/index.jsp");
        return mv;
    }
}
